package com.example.secondassignment;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.SyncStateContract.Columns;

public class CountryVisitedDataSource {

	  // Database fields
	  private SQLiteDatabase database;
	  private CountryVisitedDbHelper dbHelper;
	  private String[] allColumns = { CountryVisitedDbHelper.COLUMN_ID,
			  CountryVisitedDbHelper.COLUMN_YEAR, 
			  CountryVisitedDbHelper.COLUMN_COUNTRY };
	
	  
	  public CountryVisitedDataSource(Context context) {
	    dbHelper = new CountryVisitedDbHelper(context);
	  }

	  public List<CountryVisited> getAllCountryVisitsSortedBy(String sortBy) {
		  
		  List<CountryVisited> countryVisitsSortedByCountry = new ArrayList<CountryVisited>();
		  Cursor cursor = null;
		  
		  if(sortBy.compareTo("Country") == 0){
			  cursor = database.query(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME,
					  allColumns  , null, null, null, null, CountryVisitedDbHelper.COLUMN_COUNTRY+ " ASC");
		  } 
		  else {
			  cursor = database.query(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME,
				  allColumns  , null, null, null, null, CountryVisitedDbHelper.COLUMN_YEAR+ " DESC");
		  }
		  if(cursor != null){
			  cursor.moveToFirst();
			  while (!cursor.isAfterLast()) {
				  CountryVisited countryVisited = cursorToCountryVisited(cursor);
				  countryVisitsSortedByCountry.add(countryVisited);
				  cursor.moveToNext();
			  }
			  // Make sure to close the cursor
			  cursor.close();
		  }
		  return countryVisitsSortedByCountry;
	  }
	  
	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public CountryVisited createCountryVisited(int year, String country) {
	    ContentValues values = new ContentValues();
	    values.put(CountryVisitedDbHelper.COLUMN_YEAR, year);
	    values.put(CountryVisitedDbHelper.COLUMN_COUNTRY, country);
	    long insertId = database.insert(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME, null, values);
	    Cursor cursor = database.query(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME,
	        allColumns, CountryVisitedDbHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    CountryVisited newCountryVisited = cursorToCountryVisited(cursor);
	    cursor.close();
	    return newCountryVisited;
	  }

	  public void deleteCountryVisited(CountryVisited countryVisited) {
	    long id = countryVisited.getId();
	    System.out.println("CountryVisited deleted with id: " + id);
	    database.delete(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME, CountryVisitedDbHelper.COLUMN_ID
	    		+ " = " + id, null);
	  }
	  
	  public CountryVisited getCountryVisited(long countryVisitedId) {
		  String restrict = CountryVisitedDbHelper.COLUMN_ID + "=" + countryVisitedId;
		  Cursor cursor = database.query(true, CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME, allColumns, restrict, null, null, null, null, null);
		  if (cursor != null && cursor.getCount() > 0) {
			  cursor.moveToFirst();
			  CountryVisited countryVisited = cursorToCountryVisited(cursor);
			  return countryVisited;
		  }
		  // Make sure to close the cursor
		  cursor.close();
		  return null;
	  }
	  
	  public boolean updateCountryVisited(long countryVisitedId, String year, String country) {
		  ContentValues args = new ContentValues();
		  args.put(CountryVisitedDbHelper.COLUMN_YEAR, year);
		  args.put(CountryVisitedDbHelper.COLUMN_COUNTRY, country);

		  String restrict = CountryVisitedDbHelper.COLUMN_ID + "=" + countryVisitedId;
		  return database.update(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME, args, restrict , null) > 0;
	  } 

	  public List<CountryVisited> getAllCountryVisits() {
	    List<CountryVisited> countryVisits = new ArrayList<CountryVisited>();

	    Cursor cursor = database.query(CountryVisitedDbHelper.COUNTRYVISITED_TABLE_NAME,
	        allColumns, null, null, null, null, null);

	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      CountryVisited countryVisited = cursorToCountryVisited(cursor);
	      countryVisits.add(countryVisited);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return countryVisits;
	  }

	  private CountryVisited cursorToCountryVisited(Cursor cursor) {
		  CountryVisited countryVisited = new CountryVisited();
		  countryVisited.setId(cursor.getLong(0));
		  countryVisited.setYear(cursor.getInt(1));
		  countryVisited.setCountry(cursor.getString(2));
		  return countryVisited;
	  }
	} 
